﻿namespace ExamV5.OnLineInter
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Wuqi.Webdiyer;

    public class ManagDCList : Page
    {
        protected Button butAdd;
        protected GridView DataGridInfo;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected Label labRescordCount;
        protected LinkButton LBDel;
        protected LinkButton LBFirstPage;
        protected LinkButton LBLastPage;
        protected LinkButton LBNextPage;
        protected LinkButton LBPrevPage;
        protected LinkButton LBSelect;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected UpdatePanel UPPart;
        protected AspNetPager aspPager;

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.DataItemIndex == -1)
            //{
            //    this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
            //    this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            //}
            //else
            //{
            //    e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
            //    if ((e.Row.DataItemIndex % 2) == 0)
            //    {
            //        e.Row.Attributes.Add("bgcolor", "#FFFFFF");
            //        e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
            //    }
            //    else
            //    {
            //        e.Row.Attributes.Add("bgcolor", "#FFFFFF");
            //        e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
            //    }
            //}
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label label2 = (Label) e.Row.Cells[6].FindControl("labEdit");
                Label label = (Label) e.Row.Cells[2].FindControl("labSet");
                LinkButton button = (LinkButton) e.Row.Cells[7].FindControl("LBStart");
                button.Enabled = false;
                if (!(e.Row.Cells[4].Text.Trim() == "未开始"))
                {
                    label.Enabled = false;
                    label.Text = "<font color='Gray'>设置选项</font>";
                    label2.Text = "<font color='Gray'>修改</font>";
                    label2.Enabled = false;
                    button.Enabled = false;
                    button.ForeColor = Color.Gray;
                }
                else
                {
                    button.Attributes.Add("onclick", "javascript:{if(confirm('开启调查后，无法修改，确定要设置开始吗？')==false) return false;}");
                    label2.Text = "<a href='#' onclick=javascript:EditDCInfo('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "')>修改</a>";
                    label.Text = "<a href='#' onclick=javascript:EditItemInfo('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "')><font color='#ff6600'>设置选项</font></a>";
                    label.Enabled = true;
                    button.Enabled = true;
                }
                e.Row.Cells[1].Text = this.publicFun_0.RemoveHTML(e.Row.Cells[1].Text).Replace("&lt;BR&gt;", "").Replace("&lt;br&gt;", "").Replace("&nbsp;", "");
                if (e.Row.Cells[1].Text.Trim().Length > Convert.ToInt32(this.ViewState["SubStrLen"].ToString()))
                {
                    e.Row.Cells[1].Text = "<a href='#' onclick=javascript:LookQuestInfo('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "'); title='" + e.Row.Cells[1].Text + "'>" + e.Row.Cells[1].Text.Trim().Substring(0, Convert.ToInt32(this.ViewState["SubStrLen"].ToString())) + "…</a>";
                }
                else
                {
                    e.Row.Cells[1].Text = "<a href='#' onclick=javascript:LookQuestInfo('" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "'); title='" + e.Row.Cells[1].Text + "'>" + e.Row.Cells[1].Text.Trim() + "</a>";
                }
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                string str;
                if (!(this.ViewState["SortType"].ToString() == " Desc"))
                {
                    str = this.ViewState["SortFiled"].ToString();
                    if (str != null)
                    {
                        if (str == "TitleInfo")
                        {
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                            return;
                        }
                        if (str == "EndTime")
                        {
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↓"));
                            return;
                        }
                        if (str == "Status")
                        {
                            e.Row.Cells[4].Controls.Add(new LiteralControl("↓"));
                            return;
                        }
                        if (str == "CreatUser")
                        {
                            e.Row.Cells[5].Controls.Add(new LiteralControl("↓"));
                            return;
                        }
                    }
                    e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                    return;
                }
                str = this.ViewState["SortFiled"].ToString();
                switch (str)
                {
                    case null:
                        goto Label_05C1;

                    case "TitleInfo":
                        e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                        return;

                    //case "EndTime":
                    //    e.Row.Cells[3].Controls.Add(new LiteralControl("↑"));
                    //    break;
                }
                if (str == "EndTime")
                {
                    e.Row.Cells[3].Controls.Add(new LiteralControl("↑"));
                }
                else if (str == "Status")
                {
                    e.Row.Cells[4].Controls.Add(new LiteralControl("↑"));
                }
                else
                {
                    if (!(str == "CreatUser"))
                    {
                        goto Label_05C1;
                    }
                    e.Row.Cells[5].Controls.Add(new LiteralControl("↑"));
                }
            }
            return;
        Label_05C1:
            e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
        }

        protected void DataGridInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int num = Convert.ToInt32(this.DataGridInfo.DataKeys[e.RowIndex]["ID"].ToString());
            if (this.publicFun_0.GetValues("select ID from QuestItem where DCID=" + num, "ID") != "")
            {
                this.publicFun_0.ExecuteSql("update OnlineQuest set Status='B' where ID=" + num);
                this.method_0();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckItem", "alert('在线调查选项未输入，无法开启调查！')", true);
            }
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (!(this.ViewState["SortFiled"].ToString() == sortExpression))
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            else if (!(this.ViewState["SortType"].ToString() == " Desc"))
            {
                this.ViewState["SortType"] = " Desc";
            }
            else
            {
                this.ViewState["SortType"] = " ASC";
            }
            this.method_0();
        }

        protected void DataGridInfo_PreRender(object sender, EventArgs e)
        {
            // 使用<TH>替换<TD> 
            this.DataGridInfo.UseAccessibleHeader = true;
            //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹
            this.DataGridInfo.HeaderRow.TableSection = TableRowSection.TableHeader;
        }

        protected void aspPager_PageChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = this.aspPager.CurrentPageIndex - 1;
            this.method_0();
        }

        protected void LBDel_Click(object sender, EventArgs e)
        {
            string str = "";
            int num = 0;
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = null;
            for (int i = 0; i < this.DataGridInfo.Rows.Count; i++)
            {
                CheckBox box = (CheckBox) this.DataGridInfo.Rows[i].FindControl("CheckBoxSelect");
                if (box.Checked)
                {
                    num++;
                    str = "Delete from OnlineQuest  where ID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString() + ";delete from QuestUser where DCID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString() + ";Delete from QuestItem where DCID=" + this.DataGridInfo.DataKeys[i]["ID"].ToString();
                    command = new SqlCommand(str + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('删除在线调查[" + this.DataGridInfo.DataKeys[i]["TitleInfo"].ToString() + "]！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')", connection);
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
            }
            connection.Close();
            connection.Dispose();
            if (this.DataGridInfo.Rows.Count == num)
            {
                this.DataGridInfo.PageIndex = 0;
            }
            this.method_0();
            this.LBSelect.Text = "选中当页";
        }

        protected void LBFirstPage_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBLastPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageCount > 0)
            {
                this.DataGridInfo.PageIndex = this.DataGridInfo.PageCount - 1;
                this.method_0();
            }
        }

        protected void LBNextPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex < (this.DataGridInfo.PageCount - 1))
            {
                this.DataGridInfo.PageIndex++;
                this.method_0();
            }
        }

        protected void LBPrevPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex > 0)
            {
                this.DataGridInfo.PageIndex--;
                this.method_0();
            }
        }

        protected void LBSelect_Click(object sender, EventArgs e)
        {
            int num;
            CheckBox box;
            if (this.LBSelect.Text != "选中当页")
            {
                for (num = 0; num < this.DataGridInfo.Rows.Count; num++)
                {
                    box = (CheckBox) this.DataGridInfo.Rows[num].FindControl("CheckBoxSelect");
                    box.Checked = false;
                }
                this.LBSelect.Text = "选中当页";
            }
            else
            {
                for (num = 0; num < this.DataGridInfo.Rows.Count; num++)
                {
                    box = (CheckBox) this.DataGridInfo.Rows[num].FindControl("CheckBoxSelect");
                    if (box.Enabled)
                    {
                        box.Checked = true;
                    }
                }
                this.LBSelect.Text = "取消所选";
            }
        }

        private void method_0()
        {
            this.publicFun_0.ExecuteSql("update OnlineQuest set Status='C' where Convert(dateTime,EndTime)<getdate()");
            string selectCommandText = "";
            selectCommandText = "select ID,TitleInfo,Convert(Char(10),CreatTime,120) as CreatTime,Convert(Char(10),EndTime,120) as EndTime,CreatUser,Status=case Status when 'A' then '未开始' when 'B' then '进行中' when 'C' then '已结束' end from OnlineQuest where ID<>0";
            if (this.ViewState["ManagPower"].ToString() != "Y")
            {
                if (this.ViewState["AddPower"].ToString() == "Y")
                {
                    selectCommandText = selectCommandText + " and CreatUser='" + this.httpCookie_1.Value + "'";
                }
                else
                {
                    selectCommandText = selectCommandText + " and ID=0 ";
                    this.butAdd.Visible = false;
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('添加或管理在线调查权限未开放！')", true);
                }
            }
            else
            {
                selectCommandText = selectCommandText + " and ID<>0 ";
            }
            selectCommandText = selectCommandText + "  order by " + this.ViewState["SortFiled"].ToString() + " " + this.ViewState["SortType"].ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            //this.labRescordCount.Text = "总共&nbsp;<font color='#0000C0'><b>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</b></font>&nbsp;条记录&nbsp;<font color='#0000C0'><b>" + Convert.ToString((int) (this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b></font>&nbsp;页&nbsp;";
            this.labRescordCount.Text = "总共&nbsp;<i>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</i>&nbsp;条记录&nbsp;<b>" + Convert.ToString((int)(this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b>&nbsp;页&nbsp;";
            this.aspPager.RecordCount = dataSet.Tables["DataInfo"].Rows.Count;
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if (!((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false))
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
            else
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() == "Yes")
                    {
                        this.ViewState["SortFiled"] = "ID";
                        this.ViewState["SortType"] = " Desc";
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select AddPoll,ManagPoll from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read())
                        {
                            this.ViewState["AddPower"] = reader["AddPoll"].ToString();
                            this.ViewState["ManagPower"] = reader["ManagPoll"].ToString();
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                        this.LBDel.Attributes.Add("onclick", "javascript:{if(confirm('确定删除所有选中信息吗？')==false) return false;}");
                        string str = this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='SubStrLength'", "ParmValue").Trim();
                        if (str.Trim() == "")
                        {
                            str = "20";
                        }
                        this.ViewState["SubStrLen"] = str;
                        this.method_0();
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请与管理员联系！')", true);
                    }
                }
                else
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
            }
        }
    }
}

